import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  },
  {
    path: '/',
    redirect: '/login',
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../components/Login')
  },
  {
    path: '/home',
    name: 'Home',
    redirect: '/welcome',
    component: () => import('../components/Home'),
    children: [
      {
        path: '/welcome',
        name: 'Welcome',

        component: () => import('../components/Welcome.vue'),
      },
      {
        path: '/users',
        name: 'Users',
        component: () => import('../components/users/Users.vue'),
      },
      {
        path: '/rights',
        name: 'Rights',
        component: () => import('../components/power/Rights.vue'),
      },
      {
        path: '/roles',
        name: 'Roles',
        component: () => import('../components/power/Roles.vue'),
      },
      {
        path: '/categories',
        name: 'Categories',
        component: () => import('../components/goods/Cate.vue'),
      },
      {
        path: '/goods',
        name: 'Goods',
        component: () => import('../components/goods/List.vue'),
      },
      {
        path: '/goods/add',
        name: 'Goods_add',
        component: () => import('../components/goods/Add.vue'),
      },
      {
        path: '/params',
        name: 'Params',
        component: () => import('../components/goods/Params.vue'),
      },
      {
        path: '/orders',
        name: 'Orders',
        component: () => import('../components/order/Order.vue'),
      },
      {
        path: '/reports',
        name: 'Reports',
        component: () => import('../components/report/Report.vue'),
      },
    ]
  },
]

const router = new VueRouter({
  routes
})

//挂载路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  //获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})
export default router

